//Mon Sep 12 16:03:37 CDT 2011
import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
import java.awt.geom.*;

public class MatrixTransforming
{
	public int[][] mat;
	
	public void push(int r, int c){
		for(int i=r-1; i<=r+1; i++){
			for(int j=c-1; j<=c+1; j++){
				mat[i][j] ^= 1;
			}
		}
	}
	public int minPushes(String[] a, String[] b)
	{
		int N = a.length;
		int M = a[0].length();
		mat = new int[N][M];
		for(int i=0; i<N; i++) for(int j=0; j<M; j++) mat[i][j] = a[i].charAt(j) - '0';
		int ret = 0;
		for(int i=0; i+2<N; i++){
			for(int j=0; j+2<M; j++){
				if(mat[i][j] != b[i].charAt(j)-'0'){
					push(i+1, j+1);
					ret++;
				}
			}
		}
		for(int i=0; i<N; i++) for(int j=0; j<M; j++)
			if(mat[i][j] != b[i].charAt(j)-'0')
				return -1;
		return ret;
	}
	
	
}
//Powered by [KawigiEdit] 2.0!
